
var http = require('http');
var ttr = "127.0.0.1"
var but = 8888;
var url = require('url');
var sizename = "留言板"
http.createServer(function (request, response) {
    response.statusCode = 200 ;
   	response.setHeader('Content-Type', "text/html");
 	var fs = require("fs");
 	var ext = request.url.substr(request.url.lastIndexOf(".")+1)
 	var exts = {
 		"css" : "text/css",
 		"jpg" : "image/jpeg",
 		"html" : "text/html",
 		"js" : "text/javaScript"
 	}
 	if (exts[ext] == null) {
 		exts[ext] = "text/plain"
 	}
 	response.setHeader('Content-Type', exts[ext]);
 	var p = url.parse(request.url,true)
 	var data = ""
 	if (p.pathname == "/indexs.html"){
			try{
				 data = fs.readFileSync("."+request.url,"utf8")
			}catch(e){

			}
		var itme = fs.readFileSync("data.json","utf8");
		var itme = JSON.parse(itme);
		var s=""
			for (var i = 0; i < itme.length; i++) {
				s+='<div class="itme">'
				s+='<h5>'+itme[i].ahto+'<span>'+itme[i].acto+'</span></h5>'
				s+='<p>'+itme[i].anto+'</p>'+'<a href="" id="delete">删除</a>'
				s+='</div>'
			}
		data = data.replace(/%itme/g,s)
		}else if (p.pathname == "/indexse.html") {
			data = fs.readFileSync("."+request.url,"utf8")
		}else if (p.pathname == "/forms.html") {
			ahtuo= p.query.ahtuo
			megss= p.query.megss
		var itme = JSON.parse(fs.readFileSync("data.json","utf8"))
			itme.push({
				ahto:ahtuo,
				anto:megss,
				acto:new Date()
			});
			itme=JSON.stringify(itme)
			fs.writeFileSync("data.json",itme)
			response.writeHead(302,{"location":"/indexs.html"})
		}
			data = data.replace(/%title/g,sizename);

 	response.write(data)	
	response.end();
}).listen(but);
console.log("Server running at http://%s:%s",ttr,but); 
